<?php

namespace App\Models;


use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class Management extends Authenticatable
{

    use HasApiTokens, Notifiable;
    use SoftDeletes;


    const STATUS = [//状态
        0 => '禁用',
        1 => '正常',
    ];

    protected $table = 'management';

    protected $casts = [
        'company' => 'json',
    ];


    /**
     * 初始化头像
     */
    public static function boot()
    {
        parent::boot();

        static::saving(function($model){
            if(isset($model->attributes['company'])) {
                unset($model->attributes['company']);
            }
        });
    }


//    public function company()
//    {
//        return $this->hasMany(CompanyManagement::class, 'management_id');
//    }

    public function company()
    {
        return $this->hasManyThrough(
            Company::class,                 // 远程表
            CompanyManagement::class,          // 中间表
            'management_id',                    // 中间表对主表的关联字段
            'id',                       // 远程表对中间表的关联字段
            'id',                         // 主表对中间表的关联字段
            'company_id'            // 中间表对远程表的关联字段
        );
    }

}
